<template>
  <div class="list-wrap">
    <a-form class="search-form" :form="form">
      <a-row :gutter="gutter">
        <a-col :lg="10" :xl="6" :xxl="4">
          <a-form-item label="审核状态">
            <a-input-group class="query-item" compact>
              <a-select style="width: 100%" v-model="selectStatus">
                <a-select-option value="0">全部</a-select-option>
                <a-select-option value="1">未审核</a-select-option>
                <a-select-option value="2">已审核</a-select-option>
                <a-select-option value="3">已拒绝</a-select-option>
              </a-select>
            </a-input-group>
          </a-form-item>
        </a-col>
        <a-col :lg="14" :xl="9" :xxl="7">
          <a-form-item label>
            <a-input-group class="query-item" compact>
              <a-select style="width: 40%" v-model="defaultSearch">
                <a-select-option value="0">全部</a-select-option>
                <a-select-option value="1">商户ID</a-select-option>
                <a-select-option value="2">商户姓名</a-select-option>
                <a-select-option value="3">商户手机号</a-select-option>
                <a-select-option value="4">流水号</a-select-option>
              </a-select>
              <a-input style="width: 60%" placeholder="请输入查询条件" v-model="keyword"/>
            </a-input-group>
          </a-form-item>
        </a-col>
        <a-col :lg="12" :xl="9" :xxl="7">
          <a-form-item label="发起时间">
            <a-range-picker
              :placeholder="['开始时间', '结束时间']"
              @change="getCreateTime"
              v-model="createTime"
              class="select-range-time"
              style="width: 100%"
            />
          </a-form-item>
        </a-col>
        <a-col :lg="10" :xl="8" :xxl="6">
          <div class="top-btns">
            <a-button @click="searchPtwithdraw" type="primary">查询</a-button>
            <a-button type="primary" v-if="isBatchAudit">审核</a-button>
            <a-button @click="resetList">重置</a-button>
            <a-button type="primary" @click="exportPop"><a-icon type="download" />导出</a-button>
          </div>
        </a-col>
      </a-row>
    </a-form>
    <div class="search-result-list">
      <a-table
        :columns="columns"
        :dataSource="providerCashData"
        rowKey="id"
        :loading="loading"
        :pagination="pagination"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :scroll="scrollX"
      >
        <span slot="action" slot-scope="text, item" class="action-wrap">
          <a @click="viewDetail(item)">查看详情</a>
          <a @click="() => hander(item, 1)">审核</a>
          <a @click="() => hander(item, 2)">拒绝</a>
        </span>
      </a-table>
      <span class="selected-item">{{`选中 ${selectedRowKeys.length} 条`}}</span>
      <pagination
        :total="total"
        @loadDataList="initPageList"
        ref="pageBand"
      />
    </div>
    <div class="modal-wrap">
      <remarks
        :title="auditTitle==1?'通过审核':'拒绝审核'"
        :hint="`请确认是否${auditTitle==1?'通过审核':'拒绝审核'}?`"
        :isRefuse="auditTitle"
        ref="remarks"
        @remarksConfirm="handleAuditOk"
      />
      <!-- 导出弹窗 -->
      <export-list ref="exportBand" exportApi="merchantWithdraw"/>
    </div>
  </div>
</template>

<script>
import { getMerwithdraw, updateMerwithdraw } from '../../store/merchantApi.js'
import { mixin } from '../../utils/remaksMixin'
export default {
  name: 'MerchantCashWithdrawal',
  mixins: [mixin],
  data() {
    return {
      form: this.$form.createForm(this),
      gutter: {
        xs: 8,
        sm: 16,
        md: 16,
        lg: 24,
        xl: 24,
        xxl: 48
      },
      columns: [
        {
          dataIndex: 'index',
          title: '序号',
          fixed: 'left',
          width: 60
        },
        {
          title: '商户id',
          dataIndex: 'merchant_no'
        },
        {
          title: '商户姓名',
          dataIndex: 'merchant_name'
        },
        {
          title: '商户手机号',
          dataIndex: 'phone'
        },
        {
          title: '流水号',
          dataIndex: 'order_no'
        },
        {
          title: '提现金额(元)',
          dataIndex: 'amount'
        },
        {
          title: '手续费(元)',
          dataIndex: 'fee'
        },
        {
          title: '到账金额(元)',
          dataIndex: 'actual_amount'
        },
        {
          title: '持卡人姓名',
          dataIndex: 'card_user'
        },
        {
          title: '银行名称',
          dataIndex: 'bankname'
        },
        {
          title: '银行卡号',
          dataIndex: 'bankcard'
        },
        {
          title: '审核状态',
          dataIndex: 'status',
          customRender(status) {
            if (status === 0) {
              return '未审核'
            } else if (status === 1) {
              return '已审核'
            } else if (status === 2) {
              return '已拒绝'
            }
          }
        },
        {
          title: '发起时间',
          dataIndex: 'created_at'
        },
        {
          title: '处理时间',
          dataIndex: 'processed_at'
        },
        {
          title: '操作人',
          dataIndex: 'admin'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          width: 100,
          customRender: (text, row, index) => {
            return {
              children: (
                <div class="col-sql" title={text}>
                  {text}
                </div>
              ),
              attrs: {}
            }
          }
        },
        {
          title: '操作',
          fixed: 'right',
          width: 200,
          scopedSlots: { customRender: 'action' }
        }
      ],
      providerCashData: [],
      loading: false,
      pagination: false,
      scrollX: {},
      total: 0,
      selectedRowKeys: [],
      auditTitle: '',
      maskClosable: false,
      remarkTxt: '',
      start_time: 0,
      end_time: 0,
      createTime: [],
      selectStatus: '0',
      defaultSearch: '0',
      keyword: '',
      isBatchAudit: false
    }
  },
  methods: {
    searchPtwithdraw() {
      this.$refs.pageBand.getData()
    },
    resetList() {
      this.verifyType = '0'
      this.selectStatus = '0'
      this.createTime = []
      this.start_time = 0
      this.end_time = 0
      this.defaultSearch = '0'
      this.keyword = ''
      this.$refs.pageBand.getData()
    },
    initPageList(info) {
      this.loading = true
      getMerwithdraw({
        verifyType: this.selectStatus, // 审核状态 0全部 1 未处理 2 已审核 3已拒绝
        startTime: this.start_time,
        endTime: this.end_time,
        searchType: this.defaultSearch,
        name: this.keyword,
        page: info.page,
        num: info.pageSize
      }).then(res => {
        this.loading = false
        var array = res.data.list
        for (let i = 0; i < array.length; i++) {
          array[i]['index'] = (info.page - 1) * info.pageSize + (i + 1)
        }
        this.providerCashData = array
        if (res.data.list.length > 0) {
          this.scrollX = { x: 2500 }
        } else {
          this.scrollX = {}
        }
        // 分页器
        this.total = res.data.total_num
      })
    },
    getCreateTime(date, str) {
      if (str[0] && str[1]) {
        this.start_time = new Date(str[0] + ' 00:00:00').getTime() / 1000
        this.end_time = new Date(str[1] + ' 23:59:59').getTime() / 1000
      } else {
        this.start_time = 0
        this.end_time = 0
      }
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
      if (selectedRowKeys && selectedRowKeys.length > 0) {
        this.isBatchAudit = true
      } else {
        this.isBatchAudit = false
      }
      console.log('已选:', selectedRowKeys)
    },
    hander(item, title) {
      if (Number(item.status) === 0) {
        this.auditTitle = title
        this.editingItem = item
        this.openRemaks()
      } else {
        this.$message.warning('该记录已审核，无法重复操作！')
      }
    },
    handleAuditOk({ remarks }) {
      var data = {
        status: this.auditTitle,
        id: this.editingItem.uuid
      }
      if (remarks) {
        data.memo = remarks
      }
      updateMerwithdraw(data).then(e => {
        this.$message.success(e.data.message)
        // this.initPageList()
        this.$refs.pageBand.getEvent()
      })
    },
    // 导出按钮弹出弹窗
    exportPop () {
      this.$refs.exportBand.openPop()
    },
    viewDetail(item) {
      this.$router.push({
        path: '/menu/merchant/cashWithdrawal/detail',
        query: { id: item.uuid }
      })
    }
  }
}
</script>

<style>
.col-sql {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 130px;
}
</style>
